package com.java.jvalidator.framework.config;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.java.jvalidator.framework.TypeUtil;
import com.java.jvalidator.framework.ValidationException;
import com.java.jvalidator.framework.ValidationExceptionMsg;

public class Parameters {
	private static final String COMMA = ",";
	// <parameterName,parameterValue>
	private Map<String, Object> parameterMap = new HashMap<String, Object>();

	protected Object clone() {
		Parameters clone = new Parameters();
		for (String key : this.parameterMap.keySet()) {
			clone.addParameter(key, getParameterValue(key));
		}
		return clone;
	}

	/**
	 * 向parameterMap中添加&lt parameterName,parameterValue &gt 形式的Parameter
	 * 
	 * @param name
	 *            属性名
	 * @param value
	 *            属性值
	 */
	public void addParameter(String name, Object value) {
		if ((TypeUtil.isStringNullOrEmpty(name)) || (value == null)) {
			return;
		}
		this.parameterMap.put(name, value);
	}

	/**
	 * 向parameterMap中添加&lt parameterName,parameterValue &gt 形式的ParameterMap
	 * 
	 * @param otherParameters
	 *            &lt parameterName,parameterValue &gt 形式的map
	 */
	public void addParameters(Parameters otherParameters) {
		if ((otherParameters == null) || (otherParameters.parameterMap == null) || (otherParameters.parameterMap.keySet() == null)) {
			return;
		}
		this.parameterMap.putAll(otherParameters.parameterMap);
	}

	/**
	 * 根据传入的paramName，返回map中对应String形式的值，如果map中不存在paramName的key，则返回null
	 * 
	 * @param paramName
	 * @return
	 */
	public String getParameterValueAsString(String paramName) {
		if (paramName == null) {
			return null;
		}
		Object value = this.parameterMap.get(paramName);
		if (value == null) {
			return null;
		}
		return value.toString();
	}

	/**
	 * 根据传入的paramName，返回map中对应的值
	 * 
	 * @param paramName
	 * @return
	 */
	public Object getParameterValue(String paramName) {
		return this.parameterMap.get(paramName);
	}

	boolean hasParameter(String name) {
		if (name == null) {
			return false;
		}
		return this.parameterMap.containsKey(name);
	}

	void replaceParameterValue(Object oldValue, Object newValue) throws ValidationException {
		if ((oldValue == null) || (newValue == null)) {
			return;
		}

		if (!oldValue.getClass().getName().equals(newValue.getClass().getName())) {
			throw new ValidationException(ValidationExceptionMsg.UNSUPPORT_TYPE_ERROR);
		}
		Set<String> keys = getParameterNames();
		for (String key : keys) {
			boolean bothString = ((oldValue instanceof String)) && ((newValue instanceof String));

			if (bothString) {
				// 如果都是String，则使用replace进行字符串替换
				String oldString = (String) oldValue;
				String newString = (String) newValue;
				String old = TypeUtil.trimString(getParameterValueAsString(key));
				String result = old.replace(oldString, newString);
				if (!TypeUtil.isStringNullOrEmpty(result)) {
					addParameter(key, result);
				}
			} else {
				// 如果是非String的对象，则直接替换
				Object old = getParameterValue(key);
				if (oldValue.equals(old)) {
					this.parameterMap.put(key, newValue);
				}
			}
		}
	}

	void addCondition(String conditionProperty, String conditionValue) {
		if ((conditionProperty == null) || (conditionValue == null)) {
			return;
		}
		String property = TypeUtil.trimString(getParameterValueAsString("conditionProperty"));
		String value = TypeUtil.trimString(getParameterValueAsString("conditionValue"));
		if (!TypeUtil.isStringNullOrEmpty(property)) {
			property = property + COMMA;
			value = value + COMMA;
		}
		property = property + conditionProperty;
		value = value + conditionValue;
		// <conditionProperty,(property1,property2...)>
		// <conditionValue, (value1,value2...)>
		// 顺序上一一对应
		addParameter("conditionProperty", property);
		addParameter("conditionValue", value);
	}

	/**
	 * 返回keySet
	 * @return
	 */
	public Set<String> getParameterNames() {
		return this.parameterMap.keySet();
	}
}
